﻿using System;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Whoever.Framework.Utils;
using Whoever.Framework.CustomException;
using Whoever.Patterns.MVC.Filters;
using Whoever.Patterns.MVC.Models;

using EditorialProyectos.Website.Models;
using EditorialProyectos.Business;
using EditorialProyectos.Business.Entities;

using Whoever.Framework.Enum;

namespace EditorialProyectos.Website.Controllers
{
    public class ProjectController : Controller
    {
        public ActionResult Main()
        {
            var model = new ProjectViewModel();

            ManagerFactory.ProjectManager.FillSelectablesRelations(model);

            model.List = ManagerFactory.ProjectManager.GetByUser(User.Identity.GetUserId());
            

            model.BenefitViewModel = new BenefitViewModel();

            return View(model);
        }

        public ActionResult View(string id)
        {
            var projectViewModel = new ViewProjectViewModel();
            ////Project project = new Project() { Id = User.Identity.GetUserId() };

            //ManagerFactory.ProjectManager.FillSelectablesRelations(projectViewModel);
            //projectViewModel.Entity = project;

            return View(projectViewModel);
        }

        [AJAXExceptionHandler]
        public ActionResult RebindList()
        {
            return Json(ManagerFactory.ProjectManager.GetByUser(User.Identity.GetUserId()));
        }

        [AJAXExceptionHandler]
        public ActionResult GetHtml(int id)
        {
            return Json(ManagerFactory.ProjectManager.GetHtml(id));
        }


        [AJAXExceptionHandler]
        public ActionResult Create(Project entity)
        {
            try
            {
                entity.UserId = User.Identity.GetUserId();

                if (ModelState.IsValid)
                {
                    var id = ManagerFactory.ProjectManager.Insert(entity);
                    return Json(new AJAXResponse { Message = Localized.Message("Create", MessageType.Success), MessageType = MessageType.Success, Success = true, MustRedirect = false, ExtraData = new { id = id } });
                }
                throw new BusinessException(Localized.Message("Model", MessageType.Error));
            }
            catch (BusinessException ex)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new Exception(Localized.Message("Exception", MessageType.Error));
            }
        }

        [AJAXExceptionHandler]
        public ActionResult Update(Project entity)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var item = ManagerFactory.ProjectManager.GetById(entity);

                    if (TryUpdateModel(item))
                        ManagerFactory.ProjectManager.Update(item);

                    return Json(new AJAXResponse { Message = Localized.Message("Update", MessageType.Success), MessageType = MessageType.Success, Success = true, MustRedirect = false });
                }
                throw new BusinessException(Localized.Message("Model", MessageType.Error));
            }
            catch (BusinessException ex)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new Exception(Localized.Message("Exception", MessageType.Error));
            }
        }

        [AJAXExceptionHandler]
        public ActionResult Delete(Project entity)
        {
            try
            {
                ManagerFactory.ProjectManager.Delete(entity);

                return Json(new AJAXResponse { Message = Localized.Message("Delete", MessageType.Success), MessageType = MessageType.Success, Success = true, MustRedirect = false });
            }
            catch (Exception ex)
            {
                throw new Exception(Localized.Message("Exception", MessageType.Error));
            }
        }

        public ActionResult GetPartial(string partialName)
        {
            var model = new ProjectViewModel();

            ManagerFactory.ProjectManager.FillSelectablesRelations(model);

            return PartialView(partialName, model);
        }
    }
}
